﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using DockSample.DAO;

namespace DockSample
{
    class MethodDAO : BaseDAO
    {
        NonFunctionRequirmentDAO nfrDAO = new NonFunctionRequirmentDAO();
        public bool DeleteMethod(Method method)
        {
            try
            {
                this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from method where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", method.id));

                cmd.ExecuteNonQuery();


                this.deleteRelations(method);

                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }
        }


        public bool AddMethod(Method method,String parentId)
        {
            try
            {
           //     this.DeleteMethod(method);
             //   this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "insert into method( id ,parentId,accessModifier,returnType,methodName,parameterList,description)  "
                    + "values ( @id ,@parentId,@accessModifier,@returnType,@methodName,@parameterList,@description)";
                cmd.Parameters.Add(new SQLiteParameter("id", method.id));
                cmd.Parameters.Add(new SQLiteParameter("parentId", parentId));
                cmd.Parameters.Add(new SQLiteParameter("accessModifier", method.AccessModifier));
                cmd.Parameters.Add(new SQLiteParameter("returnType", method.ReturnType));
                cmd.Parameters.Add(new SQLiteParameter("methodName", method.MethodName));
                cmd.Parameters.Add(new SQLiteParameter("parameterList",method.ParameterList));
                cmd.Parameters.Add(new SQLiteParameter("description", method.Description));
               


                cmd.ExecuteNonQuery();

                this.saveRelations(method);

                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }

        }


        public List<Method> GetMethodList(ObjectDiagram diagram)
        {
            try
            {
                List<Method> list = new List<Method>();


                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from method where parentid = @parentid";
               
                cmd.Parameters.Add(new SQLiteParameter("parentid",diagram.Id));
         //       Console.WriteLine(cmd.CommandText + "  " + diagram.Id);
                SQLiteDataReader reader=cmd.ExecuteReader();
                while (reader.Read())
                {
           //         Console.WriteLine("--------------");
                    Method m = new Method(reader["accessModifier"].ToString(),
                        reader["returnType"].ToString(),
                        reader["methodName"].ToString(),
                        reader["parameterList"].ToString(),
                        reader["id"].ToString(),
                        reader["description"].ToString());
                    

                    m.assoInfoId = reader["assoInfoId"].ToString();
                    m.parentMethodId = reader["parentMethodId"].ToString();
                    String strIsComplex=reader["isComplex"].ToString();
                    if ("false".Equals(strIsComplex))
                    {
                        m.isComplex = false;
                    }
                    else
                    {
                        m.isComplex = true;
                    }
                    //添加NFR
                    m.MethodNFRList = nfrDAO.GetNFRByObjId(m.id);

                    list.Add(m);
                }

                return list;

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                this.CloseConnection();
            }
        }


        private void saveRelations(Method attr)
        {
            this.saveParentMethodId(attr);
            this.saveAssoStateId(attr);
            this.saveIsComplex(attr);
            this.saveNFR(attr);
        }

        private void deleteRelations(Method m)
        {
            this.DeleteNFR(m);
        }


        private void saveNFR(Method attr)
        {
            foreach (NonFunctionRequirment nfr in attr.MethodNFRList)
            {
                nfrDAO.AddNonFunctionRequirment(nfr, attr.id);
            }

        }

        private void DeleteNFR(Method m)
        {
            foreach (NonFunctionRequirment nfr in m.MethodNFRList)
            {
                nfrDAO.DeleteNonFunctionRequirment(nfr);
            }

        }

        private void saveParentMethodId(Method attr)
        {
            if (attr.parentMethod != null)
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "update Method set parentMethodId=@parentMethodId where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("parentMethodId", attr.parentMethodId));
                cmd.Parameters.Add(new SQLiteParameter("id", attr.id));

                cmd.ExecuteNonQuery();
            }
        }

        private void saveAssoStateId(Method attr)
        {
            if (attr.assoInfo != null)
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "update Method set assoInfoId=@assoInfoId where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("assoInfoId", attr.assoInfoId));
                cmd.Parameters.Add(new SQLiteParameter("id", attr.id));

                cmd.ExecuteNonQuery();
            }
        }

        private void saveIsComplex(Method attr)
        {
            SQLiteCommand cmd = this.GetCmd();
            cmd.CommandText = "update Method set isComplex=@isComplex where id=@id";
            if (attr.isComplex)
            {
                cmd.Parameters.Add(new SQLiteParameter("isComplex", "true"));
                cmd.Parameters.Add(new SQLiteParameter("id", attr.id));
            }
            else
            {
                cmd.Parameters.Add(new SQLiteParameter("isComplex", "false"));
                cmd.Parameters.Add(new SQLiteParameter("id", attr.id));
            }
            cmd.ExecuteNonQuery();
        }

       
    }
}
